#include "gtest/gtest.h"
#include "sdk/sdk.h"

using namespace SDK;

TEST(basic_test, autoTransfer_network) {
	Network &net = PCommand::net;
	net.init(6);
	net.addEdge(1, 2, 3);
	net.addEdge(1, 3, 3);
	net.addEdge(2, 3, 5);
	net.addEdge(3, 2, 4);
	net.addEdge(2, 4, 1);
	net.addEdge(3, 5, 1);
	net.addEdge(2, 5, 1);
	net.addEdge(3, 4, 2);
	net.addEdge(4, 5, 1);
	net.addEdge(5, 4, 1);
	net.addEdge(4, 6, 2);
	net.addEdge(5, 6, 4);
	EXPECT_EQ(5, net.sap(1, 6));
	// for(auto e: net.flows())
		// printf("s %d t %d w %d\n", e.s, e.t, e.w);
}

PPlayerInfo::Robot getRobot(Pos pos, int cap, int ene)
{
	PPlayerInfo::Robot robot;
	robot.pos = pos;
	robot.transport_capacity = cap;
	robot.energy = ene;
	return robot;
}

TEST(basic_test, autoTransfer_target_1) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(1, 0), 1, 1));
	info.robots.push_back(getRobot(Pos(2, 0), 1, 0));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(2, 0), 0});
	PCommand comm;
	EXPECT_EQ(1, comm.AddAutoTransfer(info, Pos(2, 0)));
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}

TEST(basic_test, autoTransfer_target_2) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(1, 0), 1, 0));
	info.robots.push_back(getRobot(Pos(2, 0), 1, 0));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(2, 0), 0});
	PCommand comm;
	EXPECT_EQ(0, comm.AddAutoTransfer(info, Pos(2, 0)));
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}

TEST(basic_test, autoTransfer_target_3) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(1, 0), 1, 2));
	info.robots.push_back(getRobot(Pos(2, 0), 1, 0));
	info.robots.push_back(getRobot(Pos(3, 0), 2, 1));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(2, 0), 0});
	info.edges.push_back((PPlayerInfo::Edge) {Pos(3, 0), Pos(2, 0), 0});
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(3, 0), 0});
	PCommand comm;
	EXPECT_EQ(3, comm.AddAutoTransfer(info, Pos(2, 0)));
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}

TEST(basic_test, autoTransfer_weight_1) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(1, 0), 3, 3));
	info.robots.push_back(getRobot(Pos(2, 0), 0, 0));
	info.robots.push_back(getRobot(Pos(3, 0), 0, 0));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(2, 0), 0});
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(3, 0), 0});
	map<Pos, int> M;
	M[Pos(2, 0)] = 100;
	M[Pos(3, 0)] = 200;
	PCommand comm;
	EXPECT_EQ(3, comm.AddAutoTransfer(info, M));
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}

TEST(basic_test, autoTransfer_weight_2) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(1, 0), 3, 3));
	info.robots.push_back(getRobot(Pos(2, 0), 0, 0));
	info.robots.push_back(getRobot(Pos(3, 0), 0, 0));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(2, 0), 0});
	info.edges.push_back((PPlayerInfo::Edge) {Pos(1, 0), Pos(3, 0), 0});
	map<Pos, int> M;
	M[Pos(2, 0)] = 1;
	M[Pos(3, 0)] = 1000;
	PCommand comm;
	EXPECT_EQ(3, comm.AddAutoTransfer(info, M));
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}

TEST(basic_test, autoTransfer_weight_3) {
	PPlayerInfo info;
	info.robots.push_back(getRobot(Pos(17, 2), 11234, 870));
	info.robots.push_back(getRobot(Pos(2, 17), 0, 0));
	info.robots.push_back(getRobot(Pos(15, 1), 0, 0));
	info.edges.push_back((PPlayerInfo::Edge) {Pos(17, 2), Pos(15, 1), 0});
	map<Pos, int> M, needs;
    needs[Pos(15, 1)] = 340;
	PCommand comm;
    comm.AddOrder(Evolve, Pos(15, 1), AttackRobot);
	comm.AddAutoTransfer(info, M, needs);
	for(auto cmd: comm.cmds)
		printf("(%d,%d)->(%d,%d) %d\n", cmd.source.x, cmd.source.y, cmd.target.x, cmd.target.y, cmd.energy);
}
